/*
 * NewBooks.java
 * 
 * Made by Olof Ekelund, Ludvig Gee, Yacoub Sattar 
 * October 19 2012.
 * 
 * Class to write newBooks.txt
 * 
 * Version 1.0
 *  
 * Copyright Olof Ekelund, Ludvig Gee, Yacoub Sattar.
 */

import java.util.ArrayList;
import java.util.Formatter;
import java.util.Scanner;

public class NewBooks {
	static ArrayList<Books> newBooks;
	static String filepath;
	
	NewBooks(){
		newBooks = new ArrayList();
		filepath = null;
	}
	
	public void setFilepath(String f){
		f.replaceAll("\\$", "\\\\");
		f += "\\";
		filepath = f;		
	}
	
	public void addBook(Books b){
		int index = 0;
		boolean bookAdded = false;
		if(newBooks.size() == 0){
			newBooks.add(0, b);
		}
		else{
			while(!bookAdded){
				if((b.getAuthor().compareToIgnoreCase(newBooks.get(index).getAuthor())) < 0){
					newBooks.add(index, b);
					bookAdded = true;
				}
				
				else if((newBooks.size() - 1) == index){
					newBooks.add(newBooks.size(), b);
					bookAdded = true;
				}
				index++;
			}
		}
	}
	
	
	public void writeListToFile(){
		try{
			Formatter fout = new Formatter(filepath + "NewBooks.txt");
			for(int i = 0; i < newBooks.size(); i++){
				fout.format("%s", newBooks.get(i).getBookAsString());
			}
			fout.close();
		}catch(Exception e){
			System.out.println("File not found");
		}
	}
	public int getListSize(){
		return newBooks.size();
	}
}